﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using OnlineMovieTicket.Services.IServices;
using OnlineMovieTicket.Services.Services;
using OnlineMovieTicket.Model;
using OnlineMovieTicket.ViewModels;
using System.Data.SqlClient;

namespace OnlineMovieTicket.Controllers
{
    [HandleError]
    public class MovieAssignmentController : Controller
    {
        IMovieAssignmentService movieAssignSrv;
        //
        // GET: /MovieAssignment/

        public MovieAssignmentController()
        {
            movieAssignSrv = new MovieAssignmentService();
        }

        public MovieAssignmentController(IMovieAssignmentService serv)
        {
            movieAssignSrv=serv;
        }

        [Authorize]
        public ActionResult Search()
        {
            IMoviesService movieSrv = new MoviesService();
            ViewData["MovieList"] = new SelectList(movieSrv.GetMovies(), "MovieCode", "MovieName");

            ICinemaService cinemaSrv = new CinemaService();
            ViewData["CinemaList"] = new SelectList(cinemaSrv.GetCinemas(), "CinemaID", "CinemaName");

            MovieAssignmentSearchModel s = new MovieAssignmentSearchModel();
            
            return View(s);
        }

        [HttpPost]
        public ActionResult Search(MovieAssignmentSearchModel s)
        {
            //IMovieAssignmentService movieAssignSrv = new MovieAssignmentService();
            IList<Model.MovieTimeTable> data= movieAssignSrv.GetMovieAssignment(s.MovieCode, s.CinemaHallID, s.ShowingDate);
            if (Request.IsAjaxRequest())
                return View("SearchResult", data);
            else
            {
                s.MovieTimeTableList = data;
                return View(s);
            }
        }


        [Authorize]
        public ActionResult CreateMovieAssignment()
        {
            IMoviesService movieSrv = new MoviesService();
            IEnumerable<Movies> movieList = movieSrv.GetMovies();
            ViewData["MovieList"] = new SelectList(movieList,"MovieCode", "MovieName");

            ICinemaService cinemaSrv = new CinemaService();
            ViewData["CinemaList"] = new SelectList(cinemaSrv.GetCinemas(), "CinemaID", "CinemaName");

          
            MovieTimeTable movietts = new MovieTimeTable();
            return View(movietts);
        }

        [HttpPost]
        public ActionResult CreateMovieAssignment(FormCollection collection)
        {
            MovieTimeTable movietts = new MovieTimeTable();
            UpdateModel(movietts);                
            
            //IMovieAssignmentService moviettSrv = new MovieAssignmentService();
            movieAssignSrv.CreateMovieAssignment(movietts);
            return RedirectToAction("Search");
        }

        public JsonResult GetCinemaHallList(int CinemaID)
        {
            JsonResult result = new JsonResult();

            ICinemaHallService cinemaHallSrv = new CinemaHallService();
            
            result.Data = new SelectList(cinemaHallSrv.GetCinemaHallsByCinema(CinemaID), "CinemaHallID", "CinemaHallName");
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }


        
        public JsonResult GetAvailableCinemaTimeTableList(int CinemaHallID, DateTime showingDate)
        {

            JsonResult result = new JsonResult();

            ICinemaTimeTableService cinemaTTSrv = new CinemaTimeTableService();

            //result.Data = new SelectList(cinemaTTSrv.GetTimeTableByCinemaHall(CinemaHallID), "ScheduleID", "StartTime");
            List<CinemaTimeTable> cinemattList= cinemaTTSrv.GetAvailableTimeTableByCinemaHall(CinemaHallID, showingDate);
            result.Data = new SelectList(cinemattList, "ScheduleID", "StartTime",cinemattList[0].CinemaHallID);
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        
        public ActionResult Delete(int id)
        {
            //IMovieAssignmentService movieAssignSrv = new MovieAssignmentService();
            movieAssignSrv.DeleteMovieAssignment(id);
            return RedirectToAction("Search");
        }
    }
}
